// JavaScript Document

var cache_url = 'http://139.51yuncai.com/cache/';
var city_arr = new Array();
var city_value = '';
var province = '';
var city = '';
var area = '';

$(document).ready(function() {
	province = $('#province').get(0);
	city = $('#city').get(0);
	area = $('#area').get(0);
	$.getJSON(cache_url + "cityjson.js", function(json) {
		if(json.code != 0) {
			alert('get data error !');
			return 0;
		}
		$(json.data).each(init_city_arr);
		full_province();
		//selectedValue();
	});
});

var init_city_arr = function(i,v) {
	city_arr.push(v.id + "|" + v.name);
}

//填充省级行政单位
var full_province = function()
{
	var cs_i=1;
	//alert(city_arr);
	//alert(city_arr[0].substring(7,city_arr[0].length));
	//alert(city_arr[0].substring(0,6));
	for(i=0;i<city_arr.length;i++)
	{
		//document.write(city_arr[i].substring(2,6)+"<br>");
		if(city_arr[i].substring(2,6)=="0000")
		{
			province.options[cs_i]=new Option(city_arr[i].substring(7,city_arr[i].length),city_arr[i].substring(0,6));
			cs_i++;
		}
	}
	//getCityValue();
}

//省级行政单位改变时填充地级行政单位
var full_city = function(province_str)
{
	fcs_i=1;
	var str_province=parseInt(province_str)/10000;
	//alert(str_province);
	//alert(city_arr);
	for (i=0;i<city_arr.length;i++)
	{
		if(city_arr[i].substring(0,2)==str_province&&city_arr[i].substring(2,6)!="0000"&&city_arr[i].substring(4,6)=="00")
		{
			city.options[fcs_i]=new Option(city_arr[i].substring(7,city_arr[i].length),city_arr[i].substring(0,6));
			fcs_i++;
		}
	}
	city.length=fcs_i;
}

//地级行政单位改变时填充县级行政单位
var full_area = function(city_str)
{
	fcs_i=1;
	var str_city=parseInt(city_str)/100;
	//alert(str_city);
	//alert(city_arr);
	for(i=0;i<city_arr.length;i++)
	{
		if(city_arr[i].substring(0,4)==str_city&&city_arr[i].substring(4,6)!="00")
		{
			//alert(city_arr[i].substring(0,4));
			//alert(city_arr[i].substring(4,6));
			area.options[fcs_i]=new Option(city_arr[i].substring(7,city_arr[i].length),city_arr[i].substring(0,6));
			fcs_i++;
		}
	}
	area.length=fcs_i;
}

//填充实际需要使用的INPUT的值
var getCityValue = function()
{
	city_value="";
	if(province.value!="")
	{
		city_value=province.value;
	}
	if(city.value!="")
	{
		city_value=city.value;
	}
	if(area.value!="")
	{
		city_value=area.value;
	}
}

//根据城市数据的默认值选种选择项
var selectedValue = function()
{
	//显示第一个地区
	for(i=1;i<=province.length;i++)
	{
		if(province.options[i-1].value.substring(0,2)==city_value.substring(0,2))
		{
			province.selectedIndex=i-1;
		}
	}
	full_city(province.value);
	//显示第二个地区
	for(i=1;i<=city.length;i++)
	{
		if(city.options[i-1].value.substring(0,4)==city_value.substring(0,4))
		{
			city.selectedIndex=i-1;
		}
	}
	full_area(city.value);
	//显示第三个地区
	for(i=1;i<=area.length;i++)
	{
		if(area.options[i-1].value==city_value)
		{
			area.selectedIndex=i-1;
		}
	}
}

var selectProvince = function(value) {
	//alert(value);
	full_city(value);
	full_area(city.value);
	getCityValue();
}

var selectCity = function(value) {
	//alert(value);
	full_area(value);
	getCityValue();
}

//获取显示城市信息
var showSelectCity = function(city_id) {
	//alert(city_arr);
	var areaId = city_id;
	if(areaId != 0 && areaId != '') {
		var cityId = getPcityId(areaId);
	}
	if(cityId != 0 && cityId != '') {
		full_area(cityId);
		var provinceId = getPcityId(cityId);
	}
	if(provinceId != 0 && provinceId != '') {
		full_city(provinceId);
		//selectProvince(provinceId);
	}
	province.value = provinceId;
	city.value = cityId;
	area.value = areaId;
}

//获取城市父ID
var getPcityId = function(sub_city_id) {
	var sub_city_id = String(sub_city_id);
	//alert(sub_city_id.substring(4,6));
	if(sub_city_id != '') {
		if(sub_city_id.substring(4,6) == '00') {
			//alert(sub_city_id.substring(0,2) * 10000);
			return sub_city_id.substring(0,2) * 10000;
		} else {
			//alert(sub_city_id.substring(0,4) * 100);
			return sub_city_id.substring(0,4) * 100;
		}
	} else {
		return 0;
	}
}